package com.service;

import com.dao.UsersJdbc;
import com.model.Manage;
import com.model.Users;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UsersService {
    UsersJdbc usersJdbc =new UsersJdbc();

    public String login(String username, String password) {
        ResultSet rs = usersJdbc.show();
        try {
            while (rs.next()) {
                // DB 数据库中的用户名
                // DB 数据库中的密码
                if(username.equals(rs.getString("user_name")) && password.equals(rs.getString("user_password"))){
                    return  username;
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return null;
    }

    public List<Users> show() {
        List<Users> list = new ArrayList<Users>();
        ResultSet resultSet = usersJdbc.show();
        try {
            while (resultSet.next()) {
                int user_id = resultSet.getInt("user_id");
                String user_name = resultSet.getString("user_name");
                String user_password = resultSet.getString("user_password");
                String user_email = resultSet.getString("user_email");
                String user_ctime = resultSet.getString("user_ctime");
                Users users = new Users(user_id, user_name, user_password,user_email,user_ctime);
                list.add(users);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void add(String user_name, String user_password, String user_email, String user_ctime) {
        usersJdbc.add(user_name, user_password, user_email, user_ctime);
    }


    public void delete(String id) {
        usersJdbc.delete(id);
    }

    public Users toupdate(String id) {
        ResultSet resultSet = usersJdbc.show();
        Users users=null;
        try {
            while (resultSet.next()) {
                int user_id = resultSet.getInt("user_id");
                if(id.equals(user_id+"")){
                    String user_name = resultSet.getString("user_name");
                    String user_password = resultSet.getString("user_password");
                    String user_email = resultSet.getString("user_email");
                    String user_ctime = resultSet.getString("user_ctime");
                    users = new Users(user_id, user_name, user_password,user_email,user_ctime);
                }
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return users;
    }

    public void update(String user_id, String user_name, String user_password,String user_email, String user_ctime) {
        usersJdbc.update(user_id, user_name, user_password, user_email, user_ctime);
    }
}
